Completed
Pull Request — master (#52)
by Sander
01:21
created

angular.controller(ꞌSetupCtrlꞌ)   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 133

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
c 2
b 0
f 0
nc 1
nop 6
dl 0
loc 133
rs 8.2857

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
/* global API */
2
3
/**
4
 * Nextcloud - passman
5
 *
6
 * @copyright Copyright (c) 2016, Sander Brand ([email protected])
7
 * @copyright Copyright (c) 2016, Marcos Zuriaga Miguel ([email protected])
8
 * @license GNU AGPL version 3 or any later version
9
 *
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Affero General Public License as
12
 * published by the Free Software Foundation, either version 3 of the
13
 * License, or (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU Affero General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Affero General Public License
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
 *
23
 */
24
25
(function () {
26
    'use strict';
27
28
    /**
29
     * @ngdoc function
30
     * @name passmanApp.controller:MainCtrl
31
     * @description
32
     * # MainCtrl
33
     * Controller of the passmanApp
34
     */
35
    angular.module('passmanExtension')
36
        .controller('SetupCtrl', ['$scope', '$timeout', '$location', '$rootScope', '$mdStepper', '$mdToast',
37
            function ($scope, $timeout, $location, $rootScope, $mdStepper, $mdToast) {
38
            $scope.settings = {
39
                nextcloud_host: '',
40
                nextcloud_username: '',
41
                nextcloud_password: '',
42
                ignoreProtocol: true,
43
                ignoreSubdomain: true,
44
                ignorePath: true,
45
                generatedPasswordLength: 12,
46
                remember_password: true,
47
                remember_vault_password: true,
48
                vault_password: '',
49
                refreshTime: 60,
50
                default_vault: {},
51
                master_password: '',
52
                disableAutoFill: false,
53
                disablePasswordPicker: false,
54
                debug: false
55
            };
56
            $scope.vaults = [];
57
58
            $scope.nextStep = function (stepName) {
59
                var steppers = $mdStepper('setup-stepper');
60
61
                $scope.saving = true;
62
                $scope.errors = [];
63
                $timeout(function () {
64
                    var step = stepName;
65
                    var check = $scope.check[step];
66
                    if (typeof check === "function") {
67
                        check(function (result) {
68
                            $scope.saving = false;
69
                            if (result) {
70
                                $scope.errors = [];
71
                                $scope.$apply();
72
                                steppers.next();
73
                            }
74
                            $timeout(function () {
75
                                $scope.errors = [];
76
                                $scope.$apply();
77
                            }, 5000);
78
                        });
79
                    }
80
                    else {
81
                        $scope.saving = false;
82
                        steppers.next();
83
                    }
84
                }, 10);
85
86
87
            };
88
            $scope.previousStep = function () {
89
                var steppers = $mdStepper('setup-stepper');
90
                steppers.back();
91
            };
92
93
            $scope.check = {
94
                server: function (callback) {
95
                    if(!$scope.settings.nextcloud_host){
96
                        $mdToast.showSimple(API.i18n.getMessage('invalid_host'));
97
                        callback(false);
98
                        return;
99
                    }
100
                    PAPI.host = $scope.settings.nextcloud_host;
101
                    PAPI.username = $scope.settings.nextcloud_username;
102
                    PAPI.password = $scope.settings.nextcloud_password;
103
                    PAPI.getVaults(function (vaults) {
104
                        if (vaults.hasOwnProperty('error')) {
105
                            $mdToast.showSimple(API.i18n.getMessage('invalid_response_from_server', [vaults.result.status, vaults.result.statusText]));
106
                            callback(false);
107
                        }
108
                        else {
109
                            $scope.vaults = vaults;
110
                            callback(true);
111
                        }
112
                        $scope.$apply();
113
                    });
114
                },
115
                vault: function (callback) {
116
                    for (var i = 0; i < $scope.vaults.length; i++) {
117
                        var vault = $scope.vaults[i];
118
                        if (vault.guid === $scope.settings.default_vault.guid) {
119
                            $scope.settings.default_vault = angular.copy(vault);
120
                            break;
121
                        }
122
                    }
123
                    try {
124
                        PAPI.decryptString($scope.settings.default_vault.challenge_password, $scope.settings.vault_password);
125
                        callback(true);
126
                    }
127
                    catch (e) {
128
                        $mdToast.showSimple(API.i18n.getMessage('invalid_vault_password'));
129
                        callback(false);
130
                    }
131
                },
132
                master: function (callback) {
133
                    if ($scope.settings.master_password.trim() !== '') {
134
                        callback(true);
135
                    } else {
136
                        $mdToast.showSimple(API.i18n.getMessage('empty_master_key'));
137
                        callback(false);
138
                    }
139
                }
140
            };
141
            $scope.saving = false;
142
143
144
            $scope.finished = function () {
145
                var settings = angular.copy($scope.settings);
146
                var master_password = settings.master_password;
147
                var master_password_remember = settings.master_password_remember;
148
                delete settings.master_password;
149
                delete settings.master_password_remember;
150
                $scope.saving = true;
151
                API.runtime.sendMessage(API.runtime.id, {
152
                    method: "setMasterPassword",
153
                    args: {password: master_password, savePassword: master_password_remember}
154
                })
155
                    .then(function () {
156
                        API.runtime.sendMessage(API.runtime.id, {
157
                            method: "saveSettings",
158
                            args: settings
159
                        }).then(function () {
160
                            setTimeout(function () {
161
                                window.location = '#!/';
162
                                $scope.saving = false;
163
                            }, 1500);
164
                        });
165
                    });
166
167
168
            };
169
        }]);
170
}());
171
172